<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: Ringo Chan <334258471@qq.com>
// +----------------------------------------------------------------------

namespace Admin\Controller;

/**
 * 鉴定费用控制器
 * 
 * @author Ringo Chan<334258471@qq.com>
 */
class IdentifyController extends AdminController{
    
    /**
     * 鉴定费用列表
     * 
     * @author Ringo Chan<334258471@qq.com>
     */
    public function index(){
        $M_product_category = M('product_category');
        
        $list = $M_product_category->order('sort ASC')->select();
        $tree = list_to_tree($list, $pk = 'id', $pid = 'pid', $child = '_');
        $this->assign('tree', $tree);
        C('_SYS_GET_CATEGORY_TREE_', true); //标记系统获取分类树模板
        $this->meta_title = '鉴定费用配置';
        $this->display();
    }

    /**
     * 显示分类树，仅支持内部调
     * 
     * @param  array $tree 分类树
     * @author Ringo Chan<334258471@qq.com>
     */
    public function tree($tree = null){
        C('_SYS_GET_CATEGORY_TREE_') || $this->_empty();
        $this->assign('tree', $tree);
        $this->display('tree');
    }
    
    /**
     * 编辑鉴定费用
     * 
     * @param string $id
     * @param number $pid
     * @author Ringo Chan<334258471@qq.com>
     */
    public function edit($id = null, $price = 0){
        $M_product_category = M('product_category');
        $M_identify = M('identify');
        
        $info = $M_product_category->where(array('id'=>$id))->find();
        !$info && $this->error('非法参数');
        
        !is_numeric($price) && $price < 0 && $this->error('鉴定费不能小于0');
    
        $info = $M_identify->where(array('product_category_id'=>$id))->find();
        $status = false;
        
        if ($info) {
            $status = $M_identify->where(array('id'=>$info['id']))->setField(array('price'=>$price, 'update_time'=>NOW_TIME));
        } else {
            $id = $M_identify->add(array('product_category_id'=>$id, 'price'=>$price, 'create_time'=>NOW_TIME, 'update_time'=>NOW_TIME));
            $status = $id > 0 ? true : false;
        }
        
        $status ? $this->success('编辑成功！', U('index')) : $this->error('未知错误');
    }
}
?>